/**
 * 
 */
package com.toncen.samepms.util;

import java.text.NumberFormat;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.toncen.samepms.system.dao.ConfigurationDao;
import com.toncen.samepms.system.domain.Configuration;
import com.sky.common.string.StringUtils;

/**
 * 描述：网站访问统计
 * 
 * @author tch
 * 
 */
public class SessionListener implements HttpSessionListener {
	/**
	 * 网站在线人数在application中的属性变量名
	 */
	private final static String ACTIVESESSIONS_APPLICATION_ATTRRIBUTE = "active_sessions";

	/**
	 * 网站总浏览人数在application中的属性变量名
	 */
	private final static String ACCUMULATIVESESSIONS_APPLICATION_ATTRRIBUTE = "accumulative_sessions";

	/**
	 * 网站访问次数更新频率
	 */
	private final static int NET_VISIT_TIME_UPDATE_FREQUENCE = 1000;

	/**
	 * 格式化
	 * 
	 * @param value
	 * @return
	 */
	public static Object format(Object value) {
		NumberFormat numberFormat = NumberFormat.getIntegerInstance();
		return numberFormat.format(value);
	}

	/**
	 * 累计数量
	 * 
	 * @return
	 */
	public synchronized static Object getAccumulativeSessions(HttpSession session) {
		Object accumulativeSessions = session.getServletContext().getAttribute(ACCUMULATIVESESSIONS_APPLICATION_ATTRRIBUTE);
		if (accumulativeSessions == null) {// 自服务器启动以来初次访问
			try {// 查询历史最高记录
				String lastAccumulativeSessions = new ConfigurationDao().getValueByName("visits_total");
				if (StringUtils.isEmpty(lastAccumulativeSessions)) {
					accumulativeSessions = new Long(0);
				} else {
					accumulativeSessions = new Long(lastAccumulativeSessions);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			session.getServletContext().setAttribute(ACCUMULATIVESESSIONS_APPLICATION_ATTRRIBUTE, accumulativeSessions);
		}
		return accumulativeSessions;
	}

	/**
	 * 在线数量
	 * 
	 * @return
	 */
	public synchronized static Object getActiveSessions(HttpSession session) {
		Object activeSessions = session.getServletContext().getAttribute(ACTIVESESSIONS_APPLICATION_ATTRRIBUTE);
		if (activeSessions == null) {// 自服务器启动以来初次访问
			activeSessions = new Long(0);
			session.getServletContext().setAttribute(ACTIVESESSIONS_APPLICATION_ATTRRIBUTE, activeSessions);
		}
		return activeSessions;
	}

	/*
	 * 创建会话时添加在线访客、总计访客数目 (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
	 */
	public void sessionCreated(HttpSessionEvent sessionEvent) {
		ServletContext context = sessionEvent.getSession().getServletContext();
		// 增加在线人数
		Object activeSessions = getActiveSessions(sessionEvent.getSession());
		activeSessions = new Long(Long.parseLong(activeSessions.toString()) + 1);
		context.setAttribute(ACTIVESESSIONS_APPLICATION_ATTRRIBUTE, activeSessions);
		// 增加访问总人数
		Object accumulativeSessions = getAccumulativeSessions(sessionEvent.getSession());
		accumulativeSessions = new Long(Long.parseLong(accumulativeSessions.toString()) + 1);
		context.setAttribute(ACCUMULATIVESESSIONS_APPLICATION_ATTRRIBUTE, accumulativeSessions);
		if (Long.parseLong(accumulativeSessions.toString()) % NET_VISIT_TIME_UPDATE_FREQUENCE == 0) {// 每访问累计NET_VISIT_TIME_UPDATE_FREQUENCE次则保存一次
			try {
				ConfigurationDao configurationDao=new ConfigurationDao();
				Configuration configuration = configurationDao.getByName("visits_total");
				configuration.setValue(accumulativeSessions.toString());
				configurationDao.updateNocache(configuration);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}		
	}

	/*
	 * 
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
	 */
	public void sessionDestroyed(HttpSessionEvent sessionEvent) {
		ServletContext context = sessionEvent.getSession().getServletContext();
		// 减少在线人数
		Object activeSessions = getActiveSessions(sessionEvent.getSession());
		if (Long.parseLong(activeSessions.toString()) > 0) {// 防止出现负数，大于0才减少人数
			activeSessions = new Long(Long.parseLong(activeSessions.toString()) - 1);
		}
		context.setAttribute(ACTIVESESSIONS_APPLICATION_ATTRRIBUTE, activeSessions);
	}
}
